//
//  LS_InfiniteCarouselView.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/9.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_InfiniteCarouselView.h"
#import <iCarousel.h>
#import "LS_BannerModel.h"
#define PAGE_OFFSET 10
@interface LS_InfiniteCarouselView()<iCarouselDelegate,iCarouselDataSource>

@property(nonatomic,strong)iCarousel  *carousel;
/**
 url string 数组
 */
@property(nonatomic,strong)NSMutableArray *imageURLStringsGroup;
/**
 无数据时背景图
 */
@property(nonatomic,strong)UIImageView *placeHolderImageView;
@end

@implementation LS_InfiniteCarouselView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self  carousel];
    }return self;
}
-(void)setAutoScrollTimeInterval:(CGFloat)autoScrollTimeInterval
{
    _autoScrollTimeInterval = autoScrollTimeInterval;
    self.carousel.autoscroll = autoScrollTimeInterval;
}
-(void)setPlaceHolder:(NSString *)placeHolder
{
    _placeHolder = placeHolder ;
    [self bringSubviewToFront:self.placeHolderImageView];
    UIImage *placeholdImage = [UIImage imageNamed:placeHolder];
    self.placeHolderImageView.image = [placeholdImage HT_setRadius:self.cornerRadius size:CGSizeMake(ScreenWidth - 2*PAGE_OFFSET, self.height)];
}
-(void)setImageUrlSignal:(RACSignal *)imageUrlSignal
{
    _imageUrlSignal = imageUrlSignal;
    @weakify(self)
    [_imageUrlSignal subscribeNext:^(id x) {
        @strongify(self)
        //如果数据大于0移除掉展位图
        if ([x count] > 0 ) {
            [self.placeHolderImageView removeFromSuperview];
            
            [x enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.imageURLStringsGroup addObject:obj];
            }];
        }
        dispatch_main_async_safe(^{
            [self.carousel reloadData];
        });
        
    }];
}

-(iCarousel *)carousel
{
    return LS_LAZY(_carousel, ({
        iCarousel *caroulse = [[iCarousel alloc]init];
        caroulse.delegate = self;
        caroulse.dataSource = self;
        caroulse.autoscroll = 0.2;
        caroulse.type = iCarouselTypeCustom;
        [self addSubview:caroulse];
        caroulse;
    }));
}
-(UIImageView *)placeHolderImageView
{
    return LS_LAZY(_placeHolderImageView, ({
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth - PAGE_OFFSET * 2, self.height)];
        imageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:imageView];
        imageView;
    }));
}
-(NSMutableArray *)imageURLStringsGroup
{
    return LS_LAZY(_imageURLStringsGroup, @[].mutableCopy);
}
-(void)dealloc
{
    self.carousel.delegate = nil;
    self.carousel.dataSource = nil;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self.carousel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
 
}
#pragma mark -
#pragma maek ---delegate datasource
-(NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return self.imageURLStringsGroup.count;
}
-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    CGFloat viewWidth = ScreenWidth - 2*PAGE_OFFSET;
    if (view == nil) {
        
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, self.height)];
        view.backgroundColor = [UIColor clearColor];
    }
    
    LS_BannerModel *model = self.imageURLStringsGroup[index];
    [((UIImageView *)view) HT_setImageWithCornerRadius:self.cornerRadius imageURL:[NSURL URLWithString:model.image_url] placeholder:self.placeHolder contentMode:UIViewContentModeScaleToFill size:CGSizeMake(viewWidth, self.height)];
    return view;
}
-(CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform{
    
    static CGFloat max_sacle = 1.0f;
    static CGFloat min_scale = 0.8f;
    if (offset <= 1 && offset >= -1) {
        float tempScale = offset < 0 ? 1+offset : 1-offset;
        float slope = (max_sacle - min_scale) / 1;
        
        CGFloat scale = min_scale + slope*tempScale;
        transform = CATransform3DScale(transform, scale, scale, 1);
    }else{
        transform = CATransform3DScale(transform, min_scale, min_scale, 1);
    }
    
    return CATransform3DTranslate(transform, offset * self.carousel.itemWidth * 1.2, 0.0, 0.0);
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            
            return YES;
        }
        case iCarouselOptionSpacing:
        {
            
            return value * 1.05;
        }
        case iCarouselOptionFadeMax:
        {
            if (self.carousel.type == iCarouselTypeCustom)
            {
                
                return 0.0;
            }
            return value;
        }
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionRadius:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems:
        {
            return value;
        }
    }
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
//    LS_BannerModel *model = self.imageURLStringsGroup[index];
//    HTWebViewModel *viewModel = [[HTWebViewModel alloc] initWithServices:nil params:@{WebTitlekey:@"",RequestURLkey:model.html_url,WebNavBarStyleTypekey:@(kWebNavBarStyleNomal)}];
//    [[HTMediatorAction sharedInstance] pushWebViewControllerWithViewModel:viewModel];
    
    if (self.clickItemOperationBlock) {
        self.clickItemOperationBlock(index);
    }
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
    if (self.itemDidScrollOperationBlock) {
        self.itemDidScrollOperationBlock(self.carousel.currentItemIndex);
    }
}

@end
